/*
 * A demo for http://ant.design/
 *
 * v0.1.0 - 2016-01-30
 * @author tx
*/

'use strict';
const http = require('http');
const express = require('express');
const router = express.Router();
const webpack = require('webpack');
const logger = require('morgan');
const favicon = require('serve-favicon');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
// const ModelProxy = require('./modelproxy/modelproxy');
// ModelProxy.init('./server/modelproxy/interface.json');

// const router = new Router();
// middleware
// webpack

const index = require('./routes/index');
app.set('views', path.join(__dirname, '../views'));
app.set('view engine', 'ejs');

// const webpackDevMiddleware = require("webpack-dev-middleware")
// const webpackHotMiddleware = require('webpack-hot-middleware')
const webpackConfig = require('../webpack.dev.config');
const compiler = webpack(webpackConfig);
app.use(require("webpack-dev-middleware")(
    compiler, {noInfo : true, publicPath : webpackConfig.output.publicPath}));

app.use(require("webpack-hot-middleware")(
    compiler, {noInfo : true, publicPath : webpackConfig.output.publicPath}));

app.use(favicon(path.join(__dirname, '../public/favicon.ico')));

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../public')));

// routes
// app.use('/', routes);
// app.use('/main', main);

app.use("/", index);
/// app.get('/main', main);
app.use("/*", index);
app.all("/*", function(req, res) {
  // 判断是否登录
  res.render('index', {title : 'React antd'});
  console.log("index");
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {message : err.message, error : err});
  });
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {message : err.message, error : {}});
});

// listen
app.listen(3001);
console.log('listening on port 3001')
